package com.ListSshang;

import java.util.ArrayList;
import java.util.InputMismatchException;
import java.util.List;
import java.util.Scanner;
import java.util.regex.Matcher;
import java.util.regex.Pattern;

public class StuMgr {
    List<Student> stuList = new ArrayList();

    /**
     * 增加学生
     */
    public void addStu() {
        while (true) {
            Scanner sc = new Scanner(System.in);
            String sid;
            //正则规定学号必须都为数字
            Pattern pattern = Pattern.compile("[0-9]*");

            idout:
            while (true) {
                try {
                    System.out.println("请输入学生的学号:");
                    Scanner sc2 = new Scanner(System.in);
                    sid = sc2.next();
                    //判断输入的学号是否满足正则
                    Matcher isNum = pattern.matcher(sid);
                    if (isNum.matches()) {
                        //判断学号是否存在
                        for (Student stu : stuList) {
                            if (sid.equals(stu.getId())) {
                                throw new InputMismatchException("");//学号存在抛出异常
                            }
                        }
                        //学号满足要求并且不存在则继续
                        break idout;
                    } else {
                        throw new Exception("");//学号不满足要求抛出异常
                    }

                } catch (InputMismatchException e) {//捕捉学号存在异常
                    System.out.println("学号已存在请重新输入!!!");
                } catch (Exception e) {//捕捉学号不满足正则异常
                    System.out.println("请正确输入学号!!!");
                }
            }

            System.out.println("请输入学生的姓名:");
            String sname = sc.next();
            //限制性别只能输入男or女
            sexout:
            while (true) {
                System.out.println("请输入学生性别(男/女):");
                String ssex = sc.next();
                if ("男".equals(ssex) || "女".equals(ssex)) {
                    Student student = new Student(sid, sname, ssex);
                    stuList.add(student);
                    System.out.println("添加成功!!!");
                    break sexout;
                } else {
                    System.out.println("请按要求输入!!!");
                }
            }

            out:
            while (true) {
                System.out.println("是否继续添加(y/n)");
                Scanner sc1 = new Scanner(System.in);
                String x1 = sc1.nextLine();
                switch (x1) {
                    case "y":
                        break out;
                    case "n":
                        return;
                    default:
                        System.out.println("选项不存在请重新输入!!!");
                        break;
                }
            }

        }

    }


    /**
     * 查询学生
     */
    public void selStu() {
        System.out.println("请输入要查询的学生姓名:");
        Scanner sc = new Scanner(System.in);
        String sname = sc.next();
        for (Student stu : stuList) {
            if (sname.equals(stu.getName())) {
                System.out.println("学号\t\t" + "姓名\t\t" + "性别");
                System.out.println(stu);
                return;
            }
        }
        System.out.println("查无此人!!!");
    }

    /**
     * 显示所有学生
     */
    public void showAll() {
        System.out.println("学号\t\t" + "姓名\t\t" + "性别");
        for (Student stu : stuList) {
            System.out.println(stu);
        }
    }

    /**
     * 删除学生
     */
    public void delStu() {
        while (true) {
            System.out.println("请输入要删除的学生姓名:");
            Scanner sc = new Scanner(System.in);
            String delname = sc.next();
            System.out.println("请确认学生姓名:");
            String rename = sc.next();
            //判断两次输入是否一致
            if (delname.equals(rename)) {
                //询问是否确认删除
                delout:
                while (true) {
                    System.out.println("确认删除吗?(y/n)");
                    String q = sc.next();
                    switch (q) {
                        case "y":
                            break delout;
                        case "n":
                            System.out.println("已取消删除操作!!!");
                            return;
                        default:
                            break;
                    }
                }
                //执行删除操作
                for (int i = 0; i < stuList.size(); i++) {
                    if (delname.equals(stuList.get(i).getName())) {
                        stuList.remove(stuList.get(i));
                        System.out.println("删除成功!!!");
                        return;
                    }
                }
                System.out.println("无此学生!!!");
                return;
            } else {
                System.out.println("两次输入不一致,请重新输入!!!");
            }
        }

    }

}
